package service

import (
	"camera_app/entity"
	"fmt"

	"gorm.io/gorm"
)

type UserRepository struct {
	User *entity.User
	DB   *gorm.DB
}

func (u *UserRepository) GetOne(user *entity.User) *entity.User {
	UserId := user.UserId
	var us entity.User
	result := u.DB.First(&us, UserId)
	// u.DB.Where(user).Find(u.User)
	if result.RowsAffected == 0 {
		return nil
	}
	return &us
}
func (u *UserRepository) Login(user *entity.User) (entity.User, error) {
	var uu []entity.User
	result := u.DB.Where("username = ?", user.Username).Where("password = ?", user.Password).Find(&uu)
	if result.Error != nil {
		return entity.User{}, result.Error
	}
	if result.RowsAffected != 1 {
		return entity.User{}, fmt.Errorf("not unique")
	}
	return uu[0], nil
}
